﻿using FantasyDraft2.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FantasyDraft2.Controllers
{
    public class DraftController : Controller
    {

        FantasyDraftEntities fantasyDB = new FantasyDraftEntities();
        //
        // GET: /Draft/

        public ActionResult Index()
        {
            DraftViewModel model;
            Object c = TempData["ds"];
            if (c != null)
            {
                model = (DraftViewModel)c;
                model.availablePlayers = fantasyDB.Players.Where<Player>(x => x.IsAvailable == true);
                model.teams = fantasyDB.Users.ToList();
                model.playerid = 0;
                model.teamid = 0;
            }
            else
            {
                model = new DraftViewModel
                {
                    availablePlayers = fantasyDB.Players.Where<Player>(x => x.IsAvailable == true),
                    teams = fantasyDB.Users.ToList(),
                    message = "",
                    playerid = 0,
                    teamid = 0
                };
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(DraftViewModel model)
        {
            int tid = model.teamid;
            int pid = model.playerid;

            IEnumerable<Player> players = fantasyDB.Players.Where(x => x.TeamId == tid);

            if (players.Count() == 5)
            {
                DropPlayerModel tempmodel = new DropPlayerModel
                {
                    playerid = pid,
                    teamid = tid,
                    players = fantasyDB.Players.Where<Player>(x => x.TeamId == tid)
                };

                TempData["dp"] = tempmodel;
                return RedirectToAction("DropPlayer");
            }
            else
            {
                Player toadd = fantasyDB.Players.SingleOrDefault<Player>(x => x.PlayerId == pid);
                toadd.TeamId = tid;
                toadd.IsAvailable = false;

                PlayerEvent addEvent = new PlayerEvent
                {
                    EventDate = DateTime.Now,
                    EventType = 3,
                    FinalTeamId = tid,
                    InitialTeamId = 0,
                    Player = toadd,
                    PlayerId = pid
                };

                fantasyDB.Entry<Player>(toadd).State = System.Data.EntityState.Modified;
                fantasyDB.PlayerEvents.Add(addEvent);

                fantasyDB.SaveChanges();

                DraftViewModel temp = new DraftViewModel
                {
                    message = "Draft successful!"
                };
                TempData["ds"] = temp;

                return RedirectToAction("Index");
            }
        }

        public ActionResult DropPlayer()
        {
            DropPlayerModel m = (DropPlayerModel)TempData["dp"];
            return View(m);
        }

        [HttpPost]
        public ActionResult DropPlayer(DropPlayerModel model, String button)
        {
            switch (button)
            {
                case "Go":
                    int drop = model.dropplayerid;
                    int pid = model.playerid;
                    int tid = model.teamid;

                    Player todrop = fantasyDB.Players.SingleOrDefault<Player>(x => x.PlayerId == drop);

                    todrop.IsAvailable = true;
                    todrop.TeamId = 0;

                    PlayerEvent dropevent = new PlayerEvent
                    {
                        EventDate = DateTime.Now,
                        EventType = 4,
                        InitialTeamId = tid,
                        FinalTeamId = 0,
                        PlayerId = drop,
                        Player = todrop
                    };

                    fantasyDB.PlayerEvents.Add(dropevent);

                    Player toAdd = fantasyDB.Players.SingleOrDefault<Player>(x => x.PlayerId == pid);

                    toAdd.IsAvailable = false;
                    toAdd.TeamId = tid;
                    toAdd.IsActive = todrop.IsActive;
                    todrop.IsActive = false;

                    PlayerEvent addEvent = new PlayerEvent
                    {
                        EventDate = DateTime.Now,
                        EventType = 3,
                        InitialTeamId = 0,
                        FinalTeamId = tid,
                        Player = toAdd,
                        PlayerId = pid
                    };

                    fantasyDB.Entry<Player>(todrop).State = System.Data.EntityState.Modified;
                    fantasyDB.Entry<Player>(toAdd).State = System.Data.EntityState.Modified;
                    fantasyDB.PlayerEvents.Add(addEvent);
                    fantasyDB.SaveChanges();

                    User u = fantasyDB.Users.SingleOrDefault<User>(x => x.TeamId == tid);


                    DraftViewModel temp = new DraftViewModel
                    {
                        message = "Draft successful! " + toAdd.Name + " drafted by " + u.TeamName + ". Cut " + todrop.Name
                    };
                    TempData["ds"] = temp;

                    return RedirectToAction("Index");
                case "Cancel":
                    DraftViewModel temp2 = new DraftViewModel
                    {
                        message = "Drafting cancelled"
                    };
                    TempData["ds"] = temp2;
                    return RedirectToAction("Index");
            }

            return RedirectToAction("Index");
        }

    }
}
